<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script src="./Stack_Queue.js"></script>
    <script>
        /**
         *  只使用stack 的  
         *          peek
         *          push
         *          pop 每次弹出栈顶元素
         *      完成队列的
         *          peek
         *          push
         *          poll 每次返回队列的第一个元素
         *  思路
         *      使用两个栈。
         *      将stackpush 的数一次pop出来之后再push到stackpop里 顺序就调换了
         */ 

        
        function TwoStacksQueue(){
            let stackPush =  new Stack();
            let stackPop = new Stack();

            this.push = function(value){
                stackPush.push(value)
                console.log(stackPush)
            }

            this.poll = function(){
                // 将stack Push的数倒腾进satckPop
                if(stackPush.empty()&&stackPop.empty()){
                    throw new Error("Queue is empty!");
                }else if(stackPop.empty()){
                    while(!stackPush.empty()){
                        stackPop.push(stackPush.pop())
                    }
                }
                return stackPop.pop();
            }

            this.peek = function(){
                if(stackPush.empty()&&stackPop.empty()){
                    throw new Error("Queue is empty!");
                }else if(stackPop.empty()){
                    while(!stackPush.empty()){
                        stackPop.push(stackPush.pop())
                    }
                }

                return stackPop.peek();
            }
        }
        let arr = new TwoStacksQueue()
        arr.push(1)
        arr.push(2)
        console.log(arr.poll())

    </script>
</body>
</html>
